package com.zh.controller.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.entity.Result;
import com.zh.pojo.Member;
import com.zh.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService iMemberService;
    /**
     * 跳转vip用户界面
     * @return
     */
    @RequestMapping("/member_list")
    public String memberList(String name, Model model,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize
    ){
        List<Member> member_list = iMemberService.findMemberAll(name,pageNum,pageSize);
        PageInfo<Member> pageInfo = new PageInfo<>(member_list);
        model.addAttribute("member_list",member_list);
        model.addAttribute("pageInfo",pageInfo);
        return "member_list";
    }

    @RequestMapping("/add_member")
    @ResponseBody
    public Result<String> addMember(Member member){
        Result<String> result = new Result();
        result.setMsg("请填写用户名");
        result.setSuccess(false);
        if (member!=null
                && member.getName()!=null
                &&!member.getName().trim().equals("")){
            int i = iMemberService.addMember(member);
            if (i>0){
                result.setMsg("添加成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("添加失败");
                return result;
            }
        }

        return result;
    }
    @RequestMapping("/delete_member")
    @ResponseBody
    @Transactional
    public Result<String> deleteMemberById(String ids){
        int num=0;
        Result<String> result = new Result();
        result.setMsg("删除失败");
        result.setSuccess(false);
        String[] strArray = ids.split(",");
        List<String> list = Arrays.asList(strArray);
        System.out.println(list);
        int i = iMemberService.deleteMemberById(list);

        if (i>0){
            result.setMsg("删除成功");
            result.setSuccess(true);
            return result;
        }
        return result;
    }

    @RequestMapping("/get_member")
    @ResponseBody
    public Result<Member> getMemberById(Integer id ,Model model){
        Result<Member> result = new Result<>();
        result.setMsg("请填写用户名");
        result.setSuccess(false);
        if (id!=null){
            Member member = iMemberService.findMemberById(id);
            result.setMsg("查询成功");
            result.setSuccess(true);
            result.setData(member);
            return result;
        }
        return result;
    }
    /**
     * 根据id修改用户信息
     */
    @RequestMapping("/update_member")
    @ResponseBody
    public Result<String> updateMember(Member member){
        Result<String> result =new Result<>();
        result.setMsg("请填写用户名");
        result.setSuccess(false);
        if(member!=null && member.getId()!=null
                && member.getName()!=null
                &&!member.getName().trim().equals("")){
            int i = iMemberService.updateMember(member);
            if (i>0){
                result.setMsg("修改成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("修改失败");
                return result;
            }
        }
        return  result;
    }

}
